package dst1.listener;

import java.util.Calendar;

import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;

import dst1.model.Computer;

public class ComputerEntityListener {

	@PrePersist
	public void setCreationDate(Object entity) {
		if (entity instanceof Computer) {
			//set date 5 seconds in the past so the @past annotation doesnt get violated
			Calendar myCal1 = Calendar.getInstance();
			myCal1.add(Calendar.SECOND, -5);
			
			Computer comp = (Computer) entity;
			comp.setCreation(myCal1.getTime());
			comp.setLastUpdate(myCal1.getTime());
		}
	}

	@PreUpdate
	public void setChangeDate(Object entity) {
		if (entity instanceof Computer) {
			//set date 5 seconds in the past so the @past annotation doesnt get violated

			Calendar myCal1 = Calendar.getInstance();
			myCal1.add(Calendar.SECOND, -5);
			
			Computer comp = (Computer) entity;
			comp.setLastUpdate(myCal1.getTime());
		}
	}
}
